<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>

<div class="sf_admin_form">
  <?php echo form_tag_for($form, '@university') ?>
    <?php echo $form->renderHiddenFields(false) ?>

    <?php if ($form->hasGlobalErrors()): ?>
      <?php echo $form->renderGlobalErrors() ?>
    <?php endif; ?>

    <?php foreach ($configuration->getFormFields($form, $form->isNew() ? 'new' : 'edit') as $fieldset => $fields): ?>
      <?php include_partial('university/form_fieldset', array('university' => $university, 'form' => $form, 'fields' => $fields, 'fieldset' => $fieldset)) ?>
    <?php endforeach; ?>

    <?php include_partial('university/form_actions', array('university' => $university, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper)) ?>
  </form>
</div>


<script type="text/javascript">
        
            jQuery(document).ready(function(){
                index = jQuery('[class*=sf_admin_form_field_faculty]').length;
                add_button();
                add_delete_link();
            });
		
            function add_button(){
                if(jQuery('.sf_admin_form_field_faculty_1').length){
                    index++;
                    jQuery('.sf_admin_form_row:last').append('<button onclick="return add_faculty(this)">Add another faculty</button>');
                }
                jQuery('._delete_:last').click(function(){
                    jQuery(this).parent().parent().remove();
                });
            }
		
            function add_delete_link(){
                jQuery('div[class*=sf_admin_form_field_faculty] > div > label').append('<div class="_delete_"></div>');
                jQuery('._delete_').each(function(k,v){
                    jQuery(v).attr('db_id',jQuery(v).parent().parent().find('input[type=hidden]:eq(0)').val());
                });
			
                jQuery('._delete_').click(function(){
                    window.location='<?php echo url_for('university/delete_faculty') ?>?id='+$(this).attr('db_id');
                });
            }
		
            function remove_button(){
                $('.sf_admin_form_row button').remove();
            }
		
            function add_faculty(){
                remove_button();
                _html = $('.sf_admin_form_field_faculty_1 > div').html();
                _new_html = str_replace('faculty_1','faculty_'+index,_html);
                _new_html = str_replace('Faculty 1','Faculty '+index,_new_html);
                $('.sf_admin_form_row:last').append( '<div>'+_new_html+'</div>' );
                $('.sf_admin_form_row:last input[id*=faculty_'+index+']').attr('value','');
                $('.sf_admin_form_row:last textarea[id*=faculty_'+index+']').attr('value','');
                add_button();
                return false;
            }
		
		
            function str_replace (search, replace, subject, count) {
                j = 0,
                temp = '',
                repl = '',
                sl = 0,        fl = 0,
                f = [].concat(search),
                r = [].concat(replace),
                s = subject,
                ra = Object.prototype.toString.call(r) === '[object Array]',        sa = Object.prototype.toString.call(s) === '[object Array]';
                s = [].concat(s);
                if (count) {
                    this.window[count] = 0;
                }
                for (i = 0, sl = s.length; i < sl; i++) {
                    if (s[i] === '') {
                        continue;
                    }        for (j = 0, fl = f.length; j < fl; j++) {
                        temp = s[i] + '';
                        repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
                        s[i] = (temp).split(f[j]).join(repl);
                        if (count && s[i] !== temp) {
                            this.window[count] += (temp.length - s[i].length) / f[j].length;
                        }
                    }
                }
                return sa ? s : s[0];
            }
        </script>